

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		bGoodsAllocation: {},
        goodsAreaList:[],
        goodsShelveList:[],
        goodsAllocation:[],
        goodsList:[],
        search:{},
        goodsShelve:{},
        goodsAllocationObj:{},
	},
    created:function(){
        this.getGoodsAreaList();
    },
	methods: {
        print:function(){
            var id = getSelectedRow();
            vm.bGoodsAllocation=getRowData(id);
            if(id == null){
                return ;
            }
            JsBarcode("#barcode1", vm.bGoodsAllocation.allocationCode);
            $("#shelveName").text(vm.bGoodsAllocation.shelveName)
            $("#goodsShelveRow").text(vm.bGoodsAllocation.goodsShelveRow)
            $("#goodsShelveCol").text(vm.bGoodsAllocation.goodsShelveCol)
            $("#allocationCode").text(vm.bGoodsAllocation.allocationCode)
            $("#print").print({
                //Use Global styles
                globalStyles : true,
                //Add link with attrbute media=print
                mediaPrint : false,
                //Print in a hidden iframe
                iframe : true,
                //Don't print this
                noPrintSelector : ".avoid-this",
                //Add this at top
                prepend : "",
                //Add this on bottom
                append : "",
                //Log to console when printing is done via a deffered callback
                deferred: $.Deferred().done(function() { console.log('Printing done', arguments); })
            });

        },
	    //获取货位信息
        getGoodsAreaList:function(){
            $.post(baseURL + "basics/bgoodsarea/getAll", {'wareHouseId':parseInt(localStorage.getItem("house"))},function(r){
                vm.goodsAreaList = r.list;
                if(vm.goodsAreaList.length>0){
                    vm.search.goodsAreaId=vm.goodsAreaList[0].id;
                    vm.search.goodsAreaName=vm.goodsAreaList[0].areaName;
                    vm.getShelveAreaList(false);
				}
            });
        },
        //获取货架信息
        getShelveAreaList:function(flag){
            $.post(baseURL + "basics/bgoodsshelves/getAll", {'goodsAreaId':vm.search.goodsAreaId},function(r){
                vm.goodsShelveList = r.list;
                if(!flag){
                    if(vm.goodsShelveList.length>0){

                        vm.search.goodsShelveId=vm.goodsShelveList[0].id;
                        vm.search.goodsShelveName=vm.goodsShelveList[0].shelveName;
                        vm.goodsShelve=vm.goodsShelveList[0];
                        vm.getGoodsAllocation();
                        vm.iniList();
                        //vm.reload();
                    }else{
                        vm.search.goodsShelveId='';
                        vm.search.goodsShelveName='';
                        vm.goodsList=[];
                        vm.list();

                    }
				}else{
                    vm.search.goodsShelveId='';
                    vm.search.goodsShelveName='';
                    vm.goodsList=[];
                    vm.list();

				}
            });
        },
        //没有选择货区的时候 将表格内容致为空
        list:function(){
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:{
                    goodsShelveId: -1,
                }

            }).trigger("reloadGrid");
        },
        //初始化表格
        iniList:function(){
            $("#jqGrid").jqGrid({
                url: baseURL + 'basics/bgoodsallocation/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true , hidden:true},
                    { label: '货位编码', name: 'allocationCode', index: 'allocation_code', width: 80 ,align:"center"},
                    { label: '货位名称', name: 'allocationName', index: 'allocation_name', width: 80 ,align:"center"},
                    { label: '属于货架', name: 'goodsShelveId', index: 'goods_shelve_id', width: 80 , hidden:true},
                    { label: '属于货架', name: 'shelveName', index: 'shelveName', width: 80 ,align:"center"},
                    { label: '条形码', name: 'barCode', index: 'bar_code', width: 80 , hidden:true},
                    { label: '货架行', name: 'goodsShelveRow', index: 'goods_shelve_row', width: 80 ,align:"center"},
                    { label: '货架列', name: 'goodsShelveCol', index: 'goods_shelve_col', width: 80 ,align:"center"},
                    { label: '创建人', name: 'updateUserId', index: 'update_user_id', width: 80 , hidden:true},
                    { label: '备注', name: 'remark', index: 'remark', width: 80 , hidden:true},
                    { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,align:"center", formatter:function(value,options,row){
                            return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}},
                    { label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 , hidden:true, formatter:function(value,options,row){
                            return new Date(value).Format('yyyy-MM-dd HH:mm:ss');}},
                    { label: '状态', name: 'isUsed', index: 'is_used', width: 80 ,align:"center", formatter: function(value, options, row){
                            return value === 1 ?
                                '<span class="label" value="1" style="background-color: #DCDCDC">无货</span>' :value === 2 ?
                                    '<span class="label " value="2"  style="background-color: #00FFFF">有货</span>':value === 3 ?
                                        '<span class="label" value="3" style="background-color: #FFFF00" >故障</span>':'<span class="label" value="0" style="background-color: #FF4500" >锁定</span>';
                        }},
                    { label: '是否启用,0未启用，1启用', name: 'isUsed', index: 'is_used', width: 80 , hidden:true},
                    { label: '是否删除，1删除，0未删除', name: 'isDel', index: 'is_del', width: 80 , hidden:true}
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPager",
                jsonReader : {
                    root: "goodsAllocationList.list",
                    page: "goodsAllocationList.pageNum",
                    total: "goodsAllocationList.pages",
                    records: "goodsAllocationList.total"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                postData:{
                    goodsShelveId: vm.search.goodsShelveId,
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },
        selectChange:function(selVal){
            this.$forceUpdate()
			this.getShelveAreaList(true)
            /*let obj = {};
            obj = this.goodsAreaList.find((item)=>{
                          return item.id === selVal;
            });*/
            var obj={};
            for (var i = 0; i < this.goodsAreaList.length ; i++) {
                if(this.goodsAreaList[i].id===selVal){
                    obj=this.goodsAreaList[i];
                }
            }

            vm.search.goodsAreaName=obj.areaName;
        },
        //点击货架的时候 查询条件
        selectChangeTwo:function(selVal){

            this.$forceUpdate()
            /*let obj = {};
            obj = this.goodsShelveList.find((item)=>{
                return item.id === selVal;
            });*/
            var obj={};
            for (var i = 0; i < this.goodsAreaList.length ; i++) {
                if(this.goodsAreaList[i].id===selVal){
                    obj=this.goodsAreaList[i];
                }
            }
            vm.search.goodsShelveName=obj.shelveName;
            vm.goodsShelve=obj;
			this.getGoodsAllocation()


        },
        //获取货位信息 通过货架
        getGoodsAllocation:function () {
            $.post(baseURL + "basics/bgoodsallocation/getAll", {'goodsShelveId':vm.search.goodsShelveId},function(r){
                vm.goodsAllocation = r.list;
               if(r.list.length>0){
                   vm.goodsAllocationObj=r.list[0];
                   vm.reload();
                   vm.getGoodsList()
               }

            });
        },
        //获取货品信息
        getGoodsList:function(){
            $.post(baseURL + "goods/bgoods/list", {'allocationId':vm.goodsAllocationObj.id,'limit':1000},function(r){
                vm.goodsList = r.goodsList.list;
            });
        },
        //点击货区信息 信息触发货品查询
        getAllocationObj:function(ind,index){
            vm.goodsAllocationObj=vm.goodsAllocation[ind*vm.goodsShelve.specsCol +index];
            vm.getGoodsList()
        },
        //左边 货区 长度自适应
        divWidth:function(ind,index){

            if(vm.search.goodsShelveId !=''&& vm.search.goodsAreaId !=''){
                //通过货架的列 获取每个的长度 -2 避免超出换行
                var width=(100/vm.goodsShelve.specsCol-2)+"%";
                var isUsed='';
                //获取状态
               if(vm.goodsAllocation[ind*vm.goodsShelve.specsCol +index] !=null ||vm.goodsAllocation[ind*vm.goodsShelve.specsCol +index] !=undefined){
                   console.log(vm.goodsAllocation[ind*vm.goodsShelve.specsCol +index])
                   console.log(ind*vm.goodsShelve.specsCol +index)
                   isUsed=vm.goodsAllocation[ind*vm.goodsShelve.specsCol +index].isUsed;
               }else {
                   return '';
               }
               //返回其对应颜色以及长度
                var backcolor=isUsed==1?'#DCDCDC':isUsed==2?'#00FFFF':isUsed==3?'#FFFF00':'#FF4500';
                var obj={
                    'width':width,
                    'background-color':backcolor
                    }
                return obj;
            }else{
                return '';
            }

        },
		/*query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bGoodsAllocation = {};
		},*/
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        //保存或者跟新
		saveOrUpdate: function (event) {
			var url = vm.bGoodsAllocation.id == null ? "basics/bgoodsallocation/save" : "basics/bgoodsallocation/update";
			
			var param = {
	                "goodsAllocation":vm.bGoodsAllocation
	                };
			
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(param),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
							vm.getGoodsList();
							vm.getGoodsAllocation();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "basics/bgoodsallocation/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
        //通过id进行或获取
		getInfo: function(id){
			$.get(baseURL + "basics/bgoodsallocation/info/"+id, function(r){
                vm.bGoodsAllocation = r.map.goodsAllocation;
                //下拉框 默认是字符串 需要转换
                vm.bGoodsAllocation.isUsed =vm.bGoodsAllocation.isUsed+'';
                if(vm.bGoodsAllocation.isUsed=='2' && vm.title=="修改"){
                    vm.showList = true;
                    alert("该货位存在物品，不能修改！！");
                }
            });
		},
        //重新加载表格
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:{
                    goodsShelveId: vm.search.goodsShelveId,
                }

            }).trigger("reloadGrid");
        },
        //查看
		detail:function(event){
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
			vm.showAdd = false;
		    vm.title = "详情";
		        
		    vm.getInfo(id);

		},
	}
});

Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "H+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}